package com.business.leaf.core.segment.repo.mapper;

import com.business.leaf.core.segment.repo.model.LeafAlloc;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName LeafAllocMapper
 * @Description 数据查询
 * @Author lgh
 * @Date 2020-04-08 16:32
 */
@Mapper
public interface LeafAllocMapper {

    /**
     * 获取所有节点内容
     * @return
     */
    @Select("SELECT biz_tag,max_id,step,description,update_time FROM leaf_alloc")
    @Results(value = {
            @Result(column = "biz_tag",property = "bizTag"),
            @Result(column = "max_id",property = "maxId"),
            @Result(column = "step",property = "step"),
            @Result(column = "description",property = "description"),
            @Result(column = "update_time",property = "updateTime")
    })
    public List<LeafAlloc> getAllLeaf();

    /**
     * 获取当前所有节点
     * @return
     */
    @Select("SELECT biz_tag FROM leaf_alloc")
    List<String> getAllTags();

    /**
     * 获取某节点内容
     * @return
     */
    @Select("SELECT biz_tag,max_id,step,description,update_time FROM leaf_alloc WHERE biz_tag=#{tag}")
    @Results(value = {
            @Result(column = "biz_tag",property = "bizTag"),
            @Result(column = "max_id",property = "maxId"),
            @Result(column = "step",property = "step"),
            @Result(column = "description",property = "description"),
            @Result(column = "update_time",property = "updateTime")
    })
    public LeafAlloc getLeafByTag(String tag);

    /**
     * 更新某业务节点的最大ID
     * @param tag
     * @return
     */
    @Update("UPDATE leaf_alloc SET  max_id=max_id+step WHERE biz_tag=#{tag}")
    public int updateLeafByTag(String tag);


    /**
     * 自定义步伐
     *
     */
    @Update("UPDATE leaf_alloc SET max_id = max_id + #{step} WHERE biz_tag = #{bizTag}")
    int updateMaxIdByCustomStep(String bizTag,long step);


    @Insert("INSERT INTO leaf_alloc(biz_tag,max_id,step,description) VALUES(#{bizTag}, max_id+#{step},#{step},#{description})")
    int insertLeafAlloc(String bizTag,long step,String description);
}
